<html>

<head>
	<title>CS143 Project 1B</title>
</head>

<body bgcolor=#0073CF>
<font face = "Arial" color = #FFD700>

<form action="query.php" method="post">
<b><u>Query:</u></b><br><br> <textarea ROWS=8 COLS=80 NAME="query"></textarea> <br>
<input type="submit" />
</form>     

<?php
	$db_connection = mysql_connect("localhost", "cs143", "");
	mysql_select_db("CS143", $db_connection);
	$queryOld=$_POST["query"];
	$query = stripslashes($queryOld);
	$rs = mysql_query($query, $db_connection);
    if ($rs != "")
    {
        $numCols = mysql_num_fields($rs);
        $numRows= mysql_numrows($rs);
    }
	mysql_close($db_connection);
?>

<br>
<br>
<b><u>Results from MySQL:</u></b>
<br>
<br>
<table border=1 cellspacing=0 cellpadding=5 bordercolor = #0F4D92>
<tr>

<?php
	for ($i = 0; $i < $numCols; $i++)
	{
		$fieldInfo[i] = mysql_fetch_field($rs);
		$fieldName[i] = $fieldInfo[i]->name;
	?>
		<td align = "center"><font face="Arial" color = #FFD700><b><?php echo $fieldName[i]; ?></b></font></td>
	<?php
	}
	if ($numCols == 0 && $query != NULL) echo "Invalid Query - Only valid SELECT statements can be used."
?>

</tr>

<?php
	while($rs != "" && $row = mysql_fetch_row($rs))
	{
?>

	<tr>

	<?php
		for ($k = 0; $k < $numCols; $k++)
		{
	?>

		<td><font face="Arial" color = #FFD700>
		<?php
		if ($row[$k] != NULL)
			echo $row[$k];
		else echo "N/A";
		?>
		</font></td>

<?php
}
?>

</tr>

<?php
}
?>

</body>
</html>
